package com.bhs.sansong.ui.mine;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.Constants;
import com.bhs.sansong.bean.ImageReturn;
import com.bhs.sansong.bean.MenuBean;
import com.bhs.sansong.bean.TabBean;
import com.bhs.sansong.bean.UserInfo;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.dialog.BottomSelectDialog;
import com.bhs.sansong.ui.dialog.BottomSelectListDialog;
import com.bhs.sansong.ui.dialog.BottomSelectiveFaceDialog;
import com.bhs.sansong.ui.dialog.CityPickerDialog;
import com.bhs.sansong.ui.dialog.MyClickListener;
import com.bhs.sansong.ui.dialog.MyClickListener2;
import com.bhs.sansong.ui.setting.ChangePhoneActivity;
import com.bhs.sansong.utils.GlideEngine;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.utils.ToastUtil;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.google.gson.Gson;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 个人信息页面
 */
public class PersonalInformationActivity extends BaseActivity implements NetCallBack, View.OnClickListener {

    private ShapeableImageView mIvHead;
    private TextView mTvChangePicture;
    private TextView mTvName;
    private TextView mTvSex;
    private TextView mTvPhone;
    private TextView mTvCompanyName;
    private TextView mTvCompanyAddress;
    private TextView mTvIndustry;
    private TextView mTvCity;


    @Override
    public int initLayout() {
        return R.layout.activity_personal_information;
    }

    @Override
    public void initView() {

        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("个人信息");
        mIvHead = findViewById(R.id.iv_head);
        mIvHead.setOnClickListener(this);
        mTvChangePicture = findViewById(R.id.tv_change_picture);
        mTvChangePicture.setOnClickListener(this);
        findViewById(R.id.ll_name).setOnClickListener(this);
        mTvName = findViewById(R.id.tv_name);
        findViewById(R.id.ll_sex).setOnClickListener(this);
        mTvSex = findViewById(R.id.tv_sex);
        findViewById(R.id.ll_phone).setOnClickListener(this);
        mTvPhone = findViewById(R.id.tv_phone);
        findViewById(R.id.ll_company_name).setOnClickListener(this);
        mTvCompanyName = findViewById(R.id.tv_company_name);
        findViewById(R.id.ll_company_address).setOnClickListener(this);
        mTvCompanyAddress = findViewById(R.id.tv_company_address);
        findViewById(R.id.ll_industry).setOnClickListener(this);
        mTvIndustry = findViewById(R.id.tv_industry);
        findViewById(R.id.ll_city).setOnClickListener(this);
        mTvCity = findViewById(R.id.tv_city);
        findViewById(R.id.ll_my_circle).setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        networkRequest(netApi.get_user_info(), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            UserInfo infoBean = new Gson().fromJson(json, UserInfo.class);
            if (infoBean.isStatus()) {
                UserInfo.DataDTO.UserInfoDTO userDTO = infoBean.getData().getUser_info();
                Glide.with(this).load(userDTO.getHeaderpic()).into(mIvHead);
                mTvName.setText(userDTO.getNickname());
                mTvSex.setText(MyUtils.getSexName(userDTO.getSex()));
                mTvPhone.setText(userDTO.getMobile());
                mTvCompanyName.setText(userDTO.getCompany_name());
                mTvCompanyAddress.setText(userDTO.getCompany_address());
                mTvIndustry.setText(userDTO.getTrade());
                mTvCity.setText(userDTO.getCity());
            }
        }else if (requestCode == 9527) {
            ImageReturn dataBean = new Gson().fromJson(json, ImageReturn.class);
            if (dataBean.isStatus()) {
                Glide.with(mContext).load(dataBean.getData().getSrc()).into(mIvHead);
                networkRequest(netApi.update_user_info("headerpic", dataBean.getData().getSrc()), PersonalInformationActivity.this, 9528);
            } else {
                loadingPopupDismiss();
                ToastUtil.show(dataBean.getMsg());
            }
        } else if (requestCode == 9528) {
            loadingPopupDismiss();
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_name:
                startActivity(new Intent(mActivity, EditTextActivity.class).putExtra("updateType", "nickname").putExtra("Content", mTvName.getText().toString()));
                break;
            case R.id.ll_company_name:
                startActivity(new Intent(mActivity, EditTextActivity.class).putExtra("updateType", "company_name").putExtra("Content", mTvCompanyName.getText().toString()));
                break;
            case R.id.ll_company_address:
                startActivity(new Intent(mActivity, EditTextActivity.class).putExtra("updateType", "company_address").putExtra("Content", mTvCompanyAddress.getText().toString()));
                break;
            case R.id.iv_head:
            case R.id.tv_change_picture:
                bottomSelect();
                break;
            case R.id.ll_sex:
                bottomSelectSex();
                break;
            case R.id.ll_city:
                bottomSelectCity();
                break;
            case R.id.ll_phone:
                startActivity(new Intent(mActivity, ChangePhoneActivity.class));
                break;
            case R.id.ll_industry:
                bottomSelectIndustry();
                break;

        }
    }

    CityPickerDialog cityPickerDialog;

    private void bottomSelectCity() {
        if (cityPickerDialog == null) {
            cityPickerDialog = new CityPickerDialog(this);
            cityPickerDialog.setMyClickListener(new MyClickListener2() {
                @Override
                public void onClick(String id, String info) {
                    String[] address = info.split(",");
                    mTvCity.setText(address[1]);
                    networkRequest(netApi.update_user_info("city", address[1]), PersonalInformationActivity.this);
                }
            });
        }

        cityPickerDialog.show();
        cityPickerDialog.cityPickerReuse.setCity(Constants.city);
    }

    private OptionsPickerView<TabBean> pvOptions;

    private void bottomSelectSex() {
        List<TabBean> list = new ArrayList<>();
        list.add(new TabBean("男", "1"));
        list.add(new TabBean("女", "0"));
        if (pvOptions == null) {
            pvOptions = new OptionsPickerBuilder(mActivity, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3, View v) {
                    mTvSex.setText(list.get(options1).getTypeName());
                    String sex = list.get(options1).getType();
                    networkRequest(netApi.update_user_info("sex", sex), PersonalInformationActivity.this);
                }
            }).build();
            pvOptions.setPicker(list);
        }
        pvOptions.show();

    }

    private BasePopupView basePopupView;
    private BottomSelectDialog bottomSelectDialog;

    private void bottomSelect() {
        if (basePopupView == null) {
            bottomSelectDialog = new BottomSelectDialog(this);
            bottomSelectDialog.showSelectPicture(1);
            bottomSelectDialog.setMyClickListener(new MyClickListener() {
                @Override
                public void onClick(View v) {
                    if (v.getId() == R.id.tv_take_picture) {
                        photograph();
                    } else if (v.getId() == R.id.tv_photo_album) {
                        photoAlbum();
                    } else if (v.getId() == R.id.tv_select_picture) {
                        selectFace();
                    }
                    basePopupView.dismiss();
                }
            });
            basePopupView = new XPopup.Builder(mActivity).asCustom(bottomSelectDialog).show();

        } else {
            basePopupView.show();
        }
    }

    BottomSelectiveFaceDialog faceDialog;

    private void selectFace() {
        if (faceDialog == null) {
            faceDialog = new BottomSelectiveFaceDialog(this);
            faceDialog.setMyClickListener(new MyClickListener2() {
                @Override
                public void onClick(String id, String info) {
                    Glide.with(mContext).load(info).into(mIvHead);
                    networkRequest(netApi.update_user_info("headerpic", info), PersonalInformationActivity.this, 9528);
                }
            });
        }
        faceDialog.show();
    }


    private void photograph() {
        PictureSelector.create(this).openCamera(SelectMimeType.ofImage()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT).forResult(new OnResultCallbackListener<LocalMedia>() {
            @Override
            public void onResult(ArrayList<LocalMedia> result) {
                MyUtils.imageUpload(mActivity, result, PersonalInformationActivity.this, 9527);
            }

            @Override
            public void onCancel() {

            }
        });
    }

    private void photoAlbum() {
        PictureSelector.create(this).openGallery(SelectMimeType.ofImage()).setMaxSelectNum(1).setImageEngine(GlideEngine.createGlideEngine()).forResult(new OnResultCallbackListener<LocalMedia>() {
            @Override
            public void onResult(ArrayList<LocalMedia> result) {
                MyUtils.imageUpload(mActivity, result, PersonalInformationActivity.this, 9527);
            }

            @Override
            public void onCancel() {

            }
        });
    }

    private BottomSelectListDialog listDialog;

    private void bottomSelectIndustry() {
        if (listDialog == null) {
            List<MenuBean> list = new ArrayList<>();
            list.add(new MenuBean("公司/工厂"));
            list.add(new MenuBean("个人"));
            list.add(new MenuBean("商户"));
            list.add(new MenuBean("取消"));
            listDialog = new BottomSelectListDialog(mActivity, list);
            listDialog.setMyClickListener(new MyClickListener2() {
                @Override
                public void onClick(String id, String info) {
                    mTvIndustry.setText(id);
                    networkRequest(netApi.update_user_info("trade", id), PersonalInformationActivity.this, 9528);
                }
            });
        }
        listDialog.show();
    }
}